package com.didispace.web;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    RestTemplate restTemplate;
    // HelloService helloService;

    private final Logger logger = Logger.getLogger(getClass());

    @RequestMapping(value = "/ribbon-consumer-user", method = RequestMethod.GET)
    public String UserConsumer() {
        long start = System.currentTimeMillis();

        StringBuilder result = new StringBuilder();

        // 而返回的 ResponseEntity对象中的 body 内容类型会根据第二个参数转换为 String 类型
        // RestTemplate restTemplate = new RestTemplate();
        // ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://USER-SERVICE/user?name={1}", String.class, "didi");
        // String body = responseEntity.getBody();

        // ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://HELLO-SERVICE/hello1?name={1}", String.class, "didi");
        // String body = responseEntity.getBody();
        // result.append(body).append("<br>");

        // 返回的 body 是一个 User 对象类型
        ResponseEntity<User> responseEntity = restTemplate.getForEntity("http://HELLO-SERVICE/hello3?name={1}", User.class, "didi");
        User body= responseEntity.getBody();
        result.append(body.getName()).append("<br>");

        long end = System.currentTimeMillis();

        logger.info("Spend time : " + (end - start) );
        return result.toString();
    }
}

// http://localhost:9000/ribbon-consumer-user